module writer_mod
    use iso_fortran_env, only: real64
    implicit none
contains
    !> eltype_code(1->shell; 2->solid): 表示壳单元或者尸体单元,NODAL_U: 节点位移(nnum,3); NODAL_F: 节点应力(nnum,3)
    subroutine ls_writer(eltype,coords,elnodes,nodal_u,nodal_f)
        use sconstants_mod
        implicit none
        integer, intent(in)         :: elnodes(:,:),eltype
        real(real64), intent(in)    :: coords(:,:),nodal_u(:,:), nodal_f(:,:)
        integer                     :: i
        character(len=19)           :: time_current  ! 存储格式化后的时间（长度=19）
        character(len=*),parameter  :: shellfmt='(I8,4I16)'
        character(len=*),parameter  :: solidfmt='(I8,8I16)'
        character(len=34)            :: odbp

        call get_ymd_hms(time_current)
        odbp=time_current//'ls_output.dyn'
        open(unit=10,file=odbp,status='replace',action='write')
        
        print *, 'Starting write Linear Static analysis results to a ==.dyn== file: ', odbp

        call get_dot_datetime(time_current)
        write(10,'(a,a)') head_info,time_current
        
        ! 写入节点坐标
        write(10,'(a)') head_node_coords
        do i=1,size(coords,1)
            write(10,'(I16,3F28.10)') i,coords(i,1),coords(i,2),coords(i,3)
        end do
        write(10,'(a)') end_head_node_coords
        write(10,'(A)') '#'
        
        ! 写入单元信息
        write(10,'(a)') head_elem_conn
        do i=1,size(elnodes,1)
            if (eltype==1) then
                write(10,shellfmt) i,elnodes(i,:)
            else if (eltype==2) then
                write(10,solidfmt) i,elnodes(i,:)
            end if
        end do
        write(10,'(a)') end_head_elem_conn
        write(10,'(A)') '#'

        ! 写入节点位移
        write(10,'(A)') head_nodal_disp
        write(10,'(A,F14.7)') head_time_p,1.0
        do i=1,size(coords,1)
            write(10,'(I16,3F30.16)') i,nodal_u(i,1),nodal_u(i,2),nodal_u(i,3)
        end do
        write(10,'(A)') end_head_nodal_disp
        write(10,'(A)') '#'
        print *, '  Nodal displacements written to file.'
        ! 写入节点应力
        write(10,'(a)') head_nodal_reactions
        write(10,'(A,F14.7)') head_time_p,1.0
        do i=1,size(coords,1)
            write(10,'(I16,3F30.16)') i,nodal_f(i,1),nodal_f(i,2),nodal_f(i,3)
        end do
        write(10,'(A)') end_head_nodal_reactions
        print *, '  Nodal reaction forces written to file.'
        close(10)
        print *, 'Finished write Linear Static analysis results'
    end subroutine ls_writer

    !> eltype_code(1->shell; 2->solid): 表示壳单元或者尸体单元,NODAL_U: 节点位移(nnum,3); NODAL_F: 节点应力(nnum,3)
    ! subroutine nf_writer(eltype,coords,elnodes,eigvals,eigvecs)
    !     use sconstants_mod
    !     implicit none
    !     integer, intent(in)         :: elnodes(:,:),eltype
    !     real(real64), intent(in)    :: coords(:,:), eigvals(:,:), eigvecs(:,:)
    !     integer                     :: i
    !     character(len=19)           :: time_current  ! 存储格式化后的时间（长度=19）
    !     character(len=*),parameter  :: shellfmt='(I8,4I16)'
    !     character(len=*),parameter  :: solidfmt='(I8,8I16)'
    !     character(len=34)            :: odbp

    !     call get_ymd_hms(time_current)
    !     odbp=time_current//'ls_output.dyn'
    !     open(unit=10,file=odbp,status='replace',action='write')
        
    !     print *, 'Starting write Linear Static analysis results to a ==.dyn== file: ', odbp

    !     call get_dot_datetime(time_current)
    !     write(10,'(a,a)') head_info,time_current
        
    !     ! 写入节点坐标
    !     write(10,'(a)') head_node_coords
    !     do i=1,size(coords,1)
    !         write(10,'(I16,3F28.10)') i,coords(i,1),coords(i,2),coords(i,3)
    !     end do
    !     write(10,'(a)') end_head_node_coords
    !     write(10,'(A)') '#'
        
    !     ! 写入单元信息
    !     write(10,'(a)') head_elem_conn
    !     do i=1,size(elnodes,1)
    !         if (eltype==1) then
    !             write(10,shellfmt) i,elnodes(i,:)
    !         else if (eltype==2) then
    !             write(10,solidfmt) i,elnodes(i,:)
    !         end if
    !     end do
    !     write(10,'(a)') end_head_elem_conn
    !     write(10,'(A)') '#'

    !     ! 写入节点位移
    !     write(10,'(A)') head_nodal_disp
    !     write(10,'(A,F14.7)') head_time_p,1.0
    !     do i=1,size(coords,1)
    !         write(10,'(I16,3F30.16)') i,nodal_u(i,1),nodal_u(i,2),nodal_u(i,3)
    !     end do
    !     write(10,'(A)') end_head_nodal_disp
    !     write(10,'(A)') '#'
    !     print *, '  Nodal displacements written to file.'
    !     ! 写入节点应力
    !     write(10,'(a)') head_nodal_reactions
    !     write(10,'(A,F14.7)') head_time_p,1.0
    !     do i=1,size(coords,1)
    !         write(10,'(I16,3F30.16)') i,nodal_f(i,1),nodal_f(i,2),nodal_f(i,3)
    !     end do
    !     write(10,'(A)') end_head_nodal_reactions
    !     print *, '  Nodal reaction forces written to file.'
    !     close(10)
    !     print *, 'Finished write Linear Static analysis results'
    ! end subroutine nf_writer

    subroutine get_dot_datetime(time_str)
        character(len=*), intent(out) :: time_str
        integer :: values(8)       ! 存储日期时间组件的数组
        character(len=4) :: year
        character(len=2) :: month, day, hour, minute, second

        ! 获取当前日期和时间
        call date_and_time(values=values)

        ! 将数值转换为字符串（补前导零）
        write(year,   '(I4.4)') values(1)  ! 年，4位数字
        write(month,  '(I2.2)') values(2)  ! 月，补零
        write(day,    '(I2.2)') values(3)  ! 日，补零
        write(hour,   '(I2.2)') values(5)  ! 时，补零
        write(minute, '(I2.2)') values(6)  ! 分，补零
        write(second, '(I2.2)') values(7)  ! 秒，补零

        ! 组合成目标格式：XXXX.XX.XX XX:XX:XX
        time_str = year//'.'//month//'.'//day//' '//hour//':'//minute//':'//second
    end subroutine get_dot_datetime
    
    subroutine get_ymd_hms(time_string)
        implicit none
        character(len=13), intent(out) :: time_string
        integer :: values(8)
        character(len=4) :: year_str
        character(len=2) :: month_str, day_str, hour_str, minute_str, second_str
        
        ! 获取当前日期和时间
        call date_and_time(values=values)
        
        ! 提取各个时间组件
        write(year_str,  '(I4.4)') values(1)   ! 年
        write(month_str, '(I2.2)') values(2)  ! 月
        write(day_str,   '(I2.2)') values(3)    ! 日
        write(hour_str,  '(I2.2)') values(5)   ! 时
        write(minute_str,'(I2.2)') values(6) ! 分
        write(second_str,'(I2.2)') values(7) ! 秒
        
        ! 组合成所需格式 aaaabbcc_ddeeff
        time_string = year_str // month_str // day_str // '_' // hour_str // minute_str
    end subroutine get_ymd_hms
end module writer_mod